package load.drawing;

import core.InPort;
import core.StdNode;
import core.TriggerPort;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

/**
 *
 * @author Ben
 */
public class DrawPolygon  extends StdNode{
    private InPort xPoints = new InPort(ArrayList.class);
    private InPort yPoints = new InPort(ArrayList.class);
    private InPort nPoints = new InPort(Integer.class);
    private InPort color = new InPort(Color.class);

    private TriggerPort draw = new TriggerPort() {
            @Override
            public void callInternal() {
                Graphics g = controller.StdDraw.getInstance().getGraphics();
                g.setColor((Color)color.getValue());
                g.drawPolygon(getArray((ArrayList)xPoints.getValue()),
                              getArray((ArrayList)yPoints.getValue()),
                              (Integer)nPoints.getValue());
            }
    };

    private int[] getArray(ArrayList arrayList){
        int[] xPointsArr = new int[arrayList.size()];
        for(int i = 0; i < xPointsArr.length; i++){
            xPointsArr[i] = Integer.valueOf(arrayList.get(i).toString());
            System.out.println(xPointsArr[i]);
        }
        return xPointsArr;
    }

    private TriggerPort fill = new TriggerPort() {
            @Override
            public void callInternal() {
                Graphics g = controller.StdDraw.getInstance().getGraphics();
                g.fillPolygon(getArray((ArrayList)xPoints.getValue()),
                              getArray((ArrayList)yPoints.getValue()),
                              (Integer)nPoints.getValue());
            }
    };

    public DrawPolygon() {
        this.autoPutPorts();
    }
}